#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{

    ui->setupUi(this);
    PetSelection dialog(this);
    if (dialog.exec() == QDialog::Accepted)
    {
        QString petType = dialog.getPetType();
        QString petName = dialog.getPetName();
        m_pet = new Pet(petType, petName, this);
        //初始化时间管理器
        m_timeManager = new TimeManager(this);
        connect(m_timeManager, &TimeManager::timeChange, this, &MainWindow::updateTime);
        //创建中央部件
        QWidget *centralWidget = new QWidget(this);
        setCentralWidget(centralWidget);

        QVBoxLayout *layout = new QVBoxLayout(centralWidget); //创建布局
        //创建时间标签
        m_timeLabel = new QLabel(this);
        layout->addWidget(m_timeLabel);
        //创建宠物标签
        m_petLabel = new QLabel(this);
        layout->addWidget(m_petLabel);
        //创建操作按钮:投喂 洗澡 玩耍
        m_feedButton = new QPushButton("喂食", this);
        connect(m_feedButton, &QPushButton::clicked, this, &MainWindow::feedButton);
        layout->addWidget(m_feedButton);
        m_cleanButton = new QPushButton("洗澡", this);
        connect(m_cleanButton, &QPushButton::clicked, this, &MainWindow::cleanButton);
        layout->addWidget(m_cleanButton);
        m_playButton = new QPushButton("玩耍", this);
        connect(m_playButton, &QPushButton::clicked, this, &MainWindow::playButton);
        layout->addWidget(m_playButton);

        centralWidget->setLayout(layout);
        updateTime();
        updateStatus();
        //设置定时器
        m_statusTimer = new QTimer(this);
        connect(m_statusTimer, &QTimer::timeout, this, &MainWindow::updateStatus);
        m_statusTimer->start(5000);
    }
    else
    {
        close();
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::feedButton()
{
    if (m_timeManager->isTime())
    {
        m_pet->feed();
        updateStatus();
    }
}

void MainWindow::cleanButton()
{
    if (m_timeManager->isTime())
    {
        m_pet->clean();
        updateStatus();
    }
}

void MainWindow::playButton()
{
    if (m_timeManager->isTime())
    {
        m_pet->play();
        updateStatus();
    }
}

void MainWindow::updateStatus()
{
    m_pet->status();
    m_petLabel->setText(QString("宠物: %! (%2)\n饥饿度: %3\n清洁度: %4\n快乐度: %5\n的等级: %6")
                            .arg(m_pet->getName())
                            .arg(m_pet->getType())
                            .arg(m_pet->getHunger())
                            .arg(m_pet->getClean())
                            .arg(m_pet->getPleasure()));
}

void MainWindow::updateTime()
{
    QString timeText = m_timeManager->isTime() ? "白天" : "夜晚";
    m_timeLabel->setText(QString("当前时间为: %1").arg(timeText));
    m_feedButton->setEnabled(m_timeManager->isTime());
    m_cleanButton->setEnabled(m_timeManager->isTime());
    m_playButton->setEnabled(m_timeManager->isTime());
}
